package com.mlm.mlmapp.adapter.view;

import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus;
import com.flyco.dialog.widget.base.BottomBaseDialog;
import com.mlm.mlmapp.R;
import com.mlm.mlmapp.adapter.view.adapter.DialogItemsAdapter;
import com.mlm.mlmapp.adapter.view.listener.DialogListener;
import com.mlm.mlmapp.cons.HTTPCons;
import com.mlm.mlmapp.utils.CommonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AppointmentRanfaDialog extends BottomBaseDialog implements
		HTTPCons {
	private Context mContext;
	private int layout;
	MyListView items;
	String id;
	RadioGroup top_group;
	List<JSONObject> list=new ArrayList<JSONObject>();
	DialogItemsAdapter adapter;
	AQuery aq;
	Button apply;
	Button cancel;
	HashMap<String, String> map = new HashMap<>();
	DialogListener l;
	String clazz="长发";
	String selectedID;
	String selectedType;
	List<JSONObject> clist=new ArrayList<JSONObject>();
	public AppointmentRanfaDialog(Context context, String stylistinfoId,DialogListener l,String selectedID,String selectedType) {
		super(context);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.l =l;
		aq = new AQuery(context);
		map.put("stylistinfoId", stylistinfoId);
		map.put("productType", PRODUCT_TYPE_RANFA);
		this.selectedID=selectedID;
		this.selectedType=selectedType;
	}

	@Override
	public View onCreateView() {
		// TODO Auto-generated method stub
		View inflate = View.inflate(context, R.layout.book_ranfa_dialog, null);
		items = (MyListView) inflate.findViewById(R.id.items);
		top_group = (RadioGroup) inflate.findViewById(R.id.top_group);
		RadioButton btn_long= (RadioButton) inflate.findViewById(R.id.btn_long);
		RadioButton btn_short= (RadioButton) inflate.findViewById(R.id.btn_short);
		RadioButton btn_mid= (RadioButton) inflate.findViewById(R.id.btn_mid);
		apply = (Button) inflate.findViewById(R.id.apply);
		cancel = (Button) inflate.findViewById(R.id.cancel);
		apply.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (-1 != adapter.selectedPosition)
					l.onApply(adapter.selectedItem,clazz);
				else
					l.onApply(null,"");
			}
		});
		cancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				l.onCancel();
			}
		});
		
		top_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				list.clear();
				switch (checkedId) {
					case R.id.btn_long:
						selectedType=PRODUCT_CLASS_RANFA_LONG;
						map.put("productClas", PRODUCT_CLASS_RANFA_LONG);
						clazz = "长发";
						break;
					case R.id.btn_mid:
						selectedType=PRODUCT_CLASS_RANFA_MID;
						map.put("productClas", PRODUCT_CLASS_RANFA_MID);
						clazz = "中发";
						break;
					default:
						clazz = "短发";
						selectedType=PRODUCT_CLASS_RANFA_SHORT;
						map.put("productClas", PRODUCT_CLASS_RANFA_SHORT);
						break;
				}
				action(selectedType);
			}
		});



		if(selectedType==null||selectedType.equals("")){
			selectedType=PRODUCT_CLASS_RANFA_LONG;
		}
		adapter = new DialogItemsAdapter(mContext, list);

		adapter.setSelected(selectedID);
		adapter.setSelectedType(selectedType);
		items.setAdapter(adapter);

		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {


				if(selectedType.equals(PRODUCT_CLASS_RANFA_LONG)){
					((RadioButton)top_group.getChildAt(0)).setChecked(true);
				}else if(selectedType.equals(PRODUCT_CLASS_RANFA_SHORT)){
					((RadioButton)top_group.getChildAt(2)).setChecked(true);
				}else{
					((RadioButton)top_group.getChildAt(1)).setChecked(true);
				}
				map.put("productClas", selectedType);

				action(selectedType);
			}
		}, 500);
		return inflate;
	}

	private void action(final String type){
		aq.progress(new AQProgressDialog(mContext)).ajax(CommonUtils.getURLStringParams(get_product, map),
				JSONArray.class, new AjaxCallback<JSONArray>() {

					@Override
					public void callback(String url, JSONArray array,
										 AjaxStatus status) {
								if (status.getCode() == -101) {
							Toast.makeText(aq.getContext(), "网络异常,请检查网络",
									Toast.LENGTH_SHORT);
							return;
						}
						list.clear();
						if (array.length() == 0) {
							try {
								list.add(new JSONObject().put("init",true));
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						for (int i = 0; i < array.length(); i++) {
							try {

								JSONObject obj=array.getJSONObject(i);
								obj.put("type", type);
								if(type.equals(PRODUCT_CLASS_RANFA_LONG))
									obj.put("clasname","长发");
								else if(type.equals(PRODUCT_CLASS_RANFA_SHORT))
									obj.put("clasname","短发");
								else
									obj.put("clasname","中发");
								list.add(obj);
							} catch (JSONException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}

						}
						adapter.notifyDataSetChanged();
					}

				});	
	}
	public void setLayout(int layout) {
		this.layout = layout;
	}

	@Override
	public boolean setUiBeforShow() {
		// TODO Auto-generated method stub
		return false;
	}

}
